package com.centit.framework.ip.agentofd.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.airplanebaseinfo.po.AirplaneBaseinfo;
import com.centit.airplanebaseinfo.service.AirplaneBaseinfoManager;
import com.centit.framework.components.CodeRepositoryUtil;
import com.centit.framework.model.basedata.IUserInfo;
import com.centit.framework.model.basedata.IUserUnit;
import com.centit.support.algorithm.DatetimeOpt;
import com.centit.support.algorithm.UuidOpt;
import com.centit.support.database.utils.QueryAndNamedParams;
import com.centit.support.database.utils.QueryUtils;
import com.centit.workflow.client.service.FlowEngineClient;
import com.centit.workflow.client.service.FlowExtendClient;
import com.centit.workflow.commons.NodeEventSupport;
import com.centit.workflow.commons.WorkflowException;
import com.centit.workflow.po.FlowInstance;
import com.centit.workflow.po.FlowVariable;
import com.centit.workflow.po.NodeInfo;
import com.centit.workflow.po.NodeInstance;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.text.SimpleDateFormat;
import java.util.*;

public class OptNodeBean implements NodeEventSupport {
    private static final Logger logger = LoggerFactory.getLogger(OptNodeBean.class);
    @Resource
    private FlowEngineClient flowEngineClient;

    @Resource
    private FlowExtendClient flowExtendClient;

    @Resource
	private AirplaneBaseinfoManager airplaneBaseinfoMag;
    /**
     * 在节点创建后的调用的的事件
     *
     * @param flowInst    流程实例
     * @param nodeInst    节点实例
     * @param nodeInfo    用户自定义操作参数
     * @param optUserCode 当前操作用户
     */
    public void runAfterCreate(FlowInstance flowInst, NodeInstance nodeInst,
                               NodeInfo nodeInfo, String optUserCode) throws WorkflowException {
        String optParm = nodeInfo.getOptParam();
        //收文流程，主办分办节点
        // 进入主办分办后，赋值主办处室
        //收文流程，进入承办人办理后，更新optbaseinfo中的数据
        if (optParm.contains("swcbrbl")) {
        	
        }
    }

    /**
     * 在节点提交前的调用的的事件
     *
     * @param flowInst    流程实例
     * @param nodeInst    节点实例
     * @param nodeInfo    用户自定义操作参数
     * @param optUserCode 当前操作用户
     */

    public void runBeforeSubmit(FlowInstance flowInst, NodeInstance nodeInst,
                                NodeInfo nodeInfo, String optUserCode) throws WorkflowException {
        String optParm = nodeInfo.getOptParam();
        if (optParm.contains("jd_jsjf") ) {
        	String recId=flowInst.getFlowOptTag();
        	//修改标志位
        	AirplaneBaseinfo airplaneBaseinfo =     			
        			airplaneBaseinfoMag.getObjectById( recId);
        	if(airplaneBaseinfo!=null){
        		airplaneBaseinfo.setStatus("2");//流程结束 使用
        		airplaneBaseinfo.setAirZt("2");
        		airplaneBaseinfoMag.mergeObject(airplaneBaseinfo);
        	}
         }else if (optParm.contains("rc_fjzx") ) {
         	String recId=flowInst.getFlowOptTag();
         	//修改标志位
         	AirplaneBaseinfo airplaneBaseinfo =     			
         			airplaneBaseinfoMag.getObjectById( recId);
         	if(airplaneBaseinfo!=null){
         		airplaneBaseinfo.setStatus("3");//流程结束 注销
         		airplaneBaseinfo.setAirZt("4");
         		airplaneBaseinfo.setOffDate(new Date());
         		airplaneBaseinfoMag.mergeObject(airplaneBaseinfo);
         	}
          }
        System.out.println("runBeforeSubmit------------------");
    }

    /**
     * 自动运行节点所指向的spring bean需要实现这个接口
     *
     * @param flowInst    流程实例
     * @param nodeInst    节点实例
     * @param nodeInfo    用户自定义操作参数
     * @param optUserCode 当前操作用户
     * @return true 需要运行后需要调用自动提交，false 则不需要，一般在函数内部已经调用submitNode
     */
    public boolean runAutoOperator(FlowInstance flowInst, NodeInstance nodeInst,
                                   NodeInfo nodeInfo, String optUserCode) throws WorkflowException {

        String optParm = nodeInfo.getOptParam();

        if ("fgzrsh".equals(optParm)) { //委托评估只要有一个领导不同意直接退回

        }
        return true;
    }

    public boolean canStepToNext(FlowInstance flowInst, NodeInstance nodeInst,
                                 NodeInfo nodeInfo, String optUserCode) throws WorkflowException {
        return true;
    }

    /**
     * 获取moduleCode的值
     *
     * @param param
     * @return
     */
    private String getModuleCode(String param) {
        if (StringUtils.isBlank(param))
            return null;
        String[] params = param.split("&");
        for (String p : params) {
            String[] codes = p.split("=");
            if (codes.length > 1) {
                if ("moduleCode".equals(codes[0]))
                    return codes[1];
                else if ("moduleCode".equals(codes[1]))
                    return codes[0];
            }
        }
        return null;
    }

}
